<?php
/*
 * Created on May 26, 2009
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 
class place_Model_Vicinity extends Clu_Db_Table_Abstract
{
    //only specify the table name, everything else inherits
	protected $_name = "place_vicinities";
	protected $_primary = array("place_id", "vicinity_place_id");
	
	public function findByPrimary($a)
	{
		$place_id = $a['0'];
		$vicinity_place_id = $a['1'];
		if (!$place_id || !$vicinity_place_id)
            return null;
        else
		    return $this->fetchRow('place_id = ' . $this->getAdapter()->quote($a, 'INTEGER') .
		    						' AND vicinity_place_id = ' . $this->getAdapter()->quote($vicinity_place_id, 'INTEGER'));
		
	}
	
	public function checkVicinity($id)
	{
		$sql = "SELECT vicinity_place_id, distance FROM place_vicinities WHERE place_id=".$this->getAdapter()->quote($id, "INTEGER");
		$result = $this->getAdapter()->query($sql);
		return $result->fetchAll();
	}
	
	public function insertRaw($data, $doValidate = false)
	{
		$keys = array('place_id','place_name','vicinity_place_id','vicinity_place_name',
                      'distance','vicinity_place_type');
        $t = clu_array_merge($keys, $data);
       	return $this->insert($t);
	}
	
	public function saveRaw($a, $data, $id, $doValidate)
    {
        if ((!$a) && (!$id))
            return false;
        else if (!$a)
            	if(!$a = $this->findByPrimary($id))
            		return false;
        
        if ($doValidate)
	    {
	    	//Validate Data here
	    }
	    
        $a->setFromArray($data);
    	return $a->save($data);
        
    }
    
    public function deleteByPrimary($a)
	{
		$delete = $this->delete('place_id IN ('.$this->getAdapter()->quote($a, 'INTEGER').')');
        return (bool) $delete;
	}
	
	public function getVicinitiesByPlaceId($placeId)
	{
		$ret = $this->fetchAll('place_id = ' . $this->getAdapter()->quote($placeId, 'INTEGER'));
		return $ret->toArray();
	}
}
 
?>
